home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cug173 / landy.lxi < prev    next >
Text File  |  1983-12-26  |  2KB  |  90 lines

  1. /*
  2.  *     An example of LEX and YACC working together
  3.  */
  4.  
  5. letter = [A-Za-z_$];
  6. digit  = [0-9];
  7. white  = [\n\r\t ];
  8.  
  9. %{
  10. #include "std.h"        /* Standard C conventions */
  11. #include "landy.h"        /* %term definitions from YACC, i.e. -h output file */
  12.  
  13. char yytext[100];        /* Communication with YACC actions */
  14. int yyival;                /*       "        "    "      "    */
  15. float yyfval;            /*       "        "    "      "    */
  16. %}
  17.  
  18. %%
  19.  
  20. letter(letter|digit)*        {gettoken(yytext, sizeof(yytext));
  21.                              printf("LEX/TCON: %s\n", yytext);
  22.                              return(TCON);}
  23.  
  24. [-]*(digit)*(digit) {{
  25.             register char *p = yytext; BOOLEAN minus = FALSE;
  26.         gettoken(yytext, sizeof(yytext));
  27.         yyival = 0;
  28.         if(*p == '-') {
  29.             ++p;
  30.             minus = TRUE;
  31.         }
  32.         do {
  33.             if(*p != ',')
  34.             yyival = 10*yyival + (*p - '0');
  35.         } while(*++p);
  36.         if(minus) yyival = -yyival;
  37.         printf("LEX/ICON: %d\n", yyival);
  38.         return(ICON);
  39.         }}
  40.  
  41. [-]*(digit)*"."(digit)* {{
  42.         register char *p = yytext; long decimal = 0; BOOLEAN minus = FALSE;
  43.         gettoken(yytext, sizeof(yytext));
  44.         yyfval = 0.0;
  45.         if(*p == '-') {
  46.             ++p;
  47.             minus = TRUE;
  48.         }
  49.         do {
  50.             switch(*p) {
  51.             case ',':       break;
  52.             case '.':       decimal = 10; break;
  53.             default :       if(!decimal) {
  54.                     yyfval = 10. * (yyfval) + (*p - '0');
  55.                     break;
  56.                     } else {
  57.                     yyfval = yyfval + (*p - '0')/((float) decimal);
  58.                     decimal *= 10;
  59.                     break;
  60.                     }
  61.             }
  62.         } while (*++p);
  63.             if(minus) yyfval = -yyfval;
  64.             printf("LEX/FCON: %f\n", yyfval);
  65.             return(FCON);
  66.         }}
  67.  
  68. white(white)*    {return(LEXSKIP);}
  69.  
  70. %%
  71.  
  72. /*
  73.  * Hand-crafted lexgetc to give newline prompt.
  74.  */
  75.  
  76. lexgetc(){
  77.     static BOOLEAN newline = TRUE;
  78.     register int c;
  79.  
  80.     if(newline)
  81.         printf(">");
  82.  
  83.     if((c = getc(lexin)) == '\n')
  84.         newline = TRUE;
  85.     else
  86.         newline = FALSE;    
  87.  
  88.     return(c);
  89. }
  90.